package cn.nc.controller;


import cn.nc.entity.User;
import cn.nc.repository.UserRepository;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Controller
public class FileController {

    @Autowired
    UserRepository userRepository;


    @GetMapping("/upload")
    public String upload(){
        return "upload/upload";
    }

    @PostMapping("/upload")
    public String uploadFile(MultipartFile[] files, Model model){


        for(MultipartFile file:files){
            if(!file.isEmpty()){
                String dirPath = "F:/IDEA/work/homework/src/main/resources/static/images/articles/";
                String fileName = file.getOriginalFilename();

                File file1 = new File(dirPath+fileName);
                if(file1.exists()){
                    model.addAttribute("thisMsg","文件已经存在");
                    return "upload/upload";
                }
                try{
                    file.transferTo(file1);
                }catch (Exception e){
                    model.addAttribute("thisMsg","上传失败");
                }
            }else{
                model.addAttribute("thisMsg","文件为空");
            }
        }
        return "upload/upload";
    }


    @GetMapping("/uploadUsers")
    public String uploadUsers(){
        return "upload/uploadUsers";
    }

    @PostMapping("/uploadUsers")
    public String uploadUserFile(MultipartFile[] files, Model model){
        for(MultipartFile file:files){
            if(!file.isEmpty()){
                String dirPath = "F:/IDEA/work/homework/src/main/resources/static/images/users/";
                String fileName = file.getOriginalFilename();

                File file1 = new File(dirPath+fileName);
                if(file1.exists()){
                    model.addAttribute("thisMsg","图片已经存在？");
                    return "upload/uploadUsers";
                }
                try{
                    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                    Integer userId = ((User)authentication.getPrincipal()).getId();
                    User user = userRepository.findById(userId).get();

                    System.out.println(fileName);
                    user.setPath(fileName);
                    userRepository.save(user);
                    file.transferTo(file1);
                }catch (Exception e){
                    model.addAttribute("thisMsg","图片上传失败了哦");
                }
            }else{
                model.addAttribute("thisMsg","图片为空");
            }
        }




        return "upload/uploadUsers";
    }

    @GetMapping("/download")
    public String download(Model model) {
        String dirPath = "F:/IDEA/work/homework/src/main/resources/static/images/articles/";
        File file = new File(dirPath);
        String[] files = file.list();
        model.addAttribute("files", files);
        return "upload/download";
    }


//    @GetMapping("/downloadfile")
//    public ResponseEntity<byte[]> downloadFile(@RequestParam("filename") String filename) throws UnsupportedEncodingException {
//        String dirPath = "F:/File/";
//        File file = new File(dirPath + File.separator + filename);
//
//        HttpHeaders httpHeaders = new HttpHeaders();
//
//        httpHeaders.setContentDispositionFormData("attachment", URLEncoder.encode(filename, "UTF-8"));
//
//        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//
//        try{
//            return new ResponseEntity<byte[]>(FileUtils., httpHeaders, HttpStatus.OK);
//        }
//
//
//
//    }

}
